package datastructure.ch03_linked.ch09_single_linkedlist;

import datastructure.ch03_linked.ch07_nodes.SingleNode;
import datastructure.ch03_linked.ch08_interface_abstract.AbstractLinkedList;

/**
 *
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class SingleLinkedList<E> extends AbstractLinkedList<E> {
    public static void main(String[] args) {
        SingleLinkedList<Integer> data = new SingleLinkedList<>();
        for (int i = 0; i <= 20; i++) {
            data.add(i, i + 1);
        }
        System.out.println(data.hasCycle(data));
        System.out.println(data);
    }

    SingleNode<E> first;

    @Override
    public int indexOf(E element) {
        SingleNode<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) return i;
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == 0) {
            first = new SingleNode<>(element, first);
        } else {
            SingleNode<E> prov = getNode(index - 1);
            prov.next = new SingleNode<>(element, prov.next);
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        SingleNode<E> node = first;
        if (index == 0) {
            first = first.next;
        } else {
            SingleNode<E> prov = getNode(index - 1);
            node = prov.next;
            prov.next = node.next;
        }
        size--;
        return node.element;
    }

    @Override
    public E set(int index, E element) {
        SingleNode<E> node = getNode(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public E get(int index) {
        return getNode(index).element;
    }

    private SingleNode<E> getNode(int index) {
        rangeCheck(index);
        SingleNode<E> node = first;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }


    public boolean hasCycle(SingleLinkedList<E> head) {
        if (head.first == null || head.first.next == null) return false;
        SingleNode<E> fast = head.first.next;
        SingleNode<E> slow = head.first;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) return true;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        SingleNode<E> node = first;
        while (node != null) {
            result.append(node);
            if (node.next != null)
                result.append("->");
            node = node.next;
        }
        return result.toString();
    }
}
